import { navigateBack } from "./common";
import { get, post, del, put,uploadFile as uploadFileRequest } from "./request";

// 修改密码接口
export const changePwdRequest = (newPwd) => {
	get('/user/changePwd', { pwd: newPwd })
		.then(res => {
			uni.showToast({
				title: '密码修改成功',
				icon: "success"
			});
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 获取用户信息接口
export const userInfo = (id) => {
	return new Promise((resolve, reject) => {
		get('/user/userInfo?id='+ id)
			.then(res => {
				resolve(res.data.user);
			})
			.catch(error => {
				reject(error);
			});
	});
};

// 登录接口
export const login = (account, password) => {
	post('/user/login', {
		account: account,
		pwd: password,
		role: '用户'
	}, { needToken: false })
		.then(res => {
			uni.setStorageSync('token', res.data.token);
			uni.setStorageSync('userId', res.data.user.id);
			uni.reLaunch({
				url: "/pages/index/index"
			});
		})
		.catch(error => {
			// 错误处理已在请求拦截器中完成
			if (error.code === 500) {
				uni.showToast({
					title: '账号不存在或密码错误,请重试',
					icon: 'none',
					duration: 2500
				});
			}
		});
};

// 保存用户信息方法
export const saveInfo = (userData) => {
	post('/user/update', userData)
		.then(res => {
			uni.showToast({
				title: '保存成功',
				icon: 'success'
			});
			navigateBack();
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 删除文件方法
export const deleteFile = (path) => {
	del('/user/deleteFile', { filepath: path })
		.then(res => {
			navigateBack();
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 注册接口
export const registerInfo = (form, userData) => {
	form.value.validate().then(res => {
		post('/user/register', userData, { needToken: false })
			.then(res => {
				uni.showToast({
					title: '注册成功',
					icon: 'success'
				});
				navigateBack();
			})
			.catch(() => {
				// 错误处理已在请求拦截器中完成
			});
	}).catch(err => {
		console.log('err', err);
	});
};

// 邮箱获取验证码功能
export const getMailCode = (email) => {
	get('/mail/getCode', { userEmail: email }, { needToken: false })
		.then(res => {
			uni.showToast({
				title: '邮箱验证码发送成功',
				icon: 'success'
			});
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 检查邮箱验证码接口
export const checkMailCode = (email, code) => {
	return new Promise((resolve, reject) => {
		get('/mail/checkCode', { userEmail: email, code: code }, { needToken: false })
			.then(res => {
				uni.showToast({
					title: '验证码验证成功',
					icon: 'success'
				});
				resolve(true);
			})
			.catch(error => {
				reject(error);
			});
	});
};

// 重置密码接口
export const resetMailPwd = (email, pwd) => {
	get('/mail/resetPwd', { userEmail: email, pwd: pwd }, { needToken: false })
		.then(res => {
			uni.showToast({
				title: '密码修改成功',
				icon: 'success'
			});
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 上传单个文件接口
export const uploadFile = (path) => {
	return new Promise((resolve, reject) => {
		uploadFileRequest('/user/uploadImage', path, 'file', {})
			.then(res => {
				const uploadedFilePath = 'http://127.0.0.1:8081/' + res.data.imgUrl;
				resolve(uploadedFilePath);
			})
			.catch(error => {
				reject(error);
			});
	});
};

// 上传多个文件接口
export const uploadBatch = (paths) => {
	let tasks = [];
	paths.forEach((path) => {
		tasks.push(uploadFile(path));
	});
	return Promise.all(tasks);
};

// 提交入党申请书接口
export const submitApplication = (data) => {
	post('/party/application/submit', data)
		.then(res => {
			uni.showToast({
				title: '入党申请书提交成功',
				icon: "success"
			});
			navigateBack();
		})
		.catch(() => {
			// 错误处理已在请求拦截器中完成
		});
};

// 提交入党志愿书接口
export const submitVolunteer = (data) => {
	post('/party/volunteer/submit', data)
		.then(res => {
			uni.showToast({
				title: '入党志愿书提交成功',
				icon: "success"
			});
			navigateBack();
		})
		.catch((err) => {
			
			console.log(err);
		});
};

// 获取用户状态接口
export const userStatus = () => {
	return new Promise((resolve, reject) => {
		get('/user/status')
			.then(res => {
				resolve(res.data);
			})
			.catch(error => {
				reject(error);
			});
	});
};

// 培养联系人获得自己带的考察对象
export const contactGetUser = (id) => {
	return get('/contactUser/userIds/'+id);
}

// 获得自己的培养联系人
export const userGetContact = (id) => {
	return get('/contactUser/contactIds/'+id);
}

// 获取用户信息接口(改良版本)
export const userInfoAsync = (id) => {
	return get('/user/userInfo?id='+ id);
};

// 获取用户信息接口(改良版本)
export const uploadInspection = (data) => {
	return post('/inspectionLog',data);
};

// 获取当前支部下的所有成员
export const getDeptUsers = (id) => {
	return get('/user/selectSameDeptPeople/' + id);
}

// 提交培养联系人
export const sumbitContactUsers = (data) => {
	return post('/contactUser',data);
}

// 删除提交培养联系人
export const deleteContactUsers = (contactId,userId) => {
	return del('/contactUser/' + contactId + '/' +userId);
}



// 入党介绍人获得自己带的考察对象
export const introduceGetUser = (id) => {
	return get('/introducerUser/userIds/'+id);
}

// 获得自己的入党介绍人
export const userGetintroduce = (id) => {
	return get('/introducerUser/introducerIds/'+id);
}

// 提交入党介绍人表单
export const sumbitintroduceUser = (data) => {
	return post('/introducerUser',data);
}

// 删除入党介绍人
export const deleteIntroduceUsers = (introduceId,userId) => {
	return del('/introducerUser/' + introduceId + '/' +userId);
}

// 提交谈话信息
export const submitTalkInfo = (data) => {
	return post('/talk/saveOrUpdate',data);
}

// 获取所有入党申请书列表
export const getApplicationList = (data) => {
	return post('/party/application/selectPage',data);
}

// 保存入党申请书的修改
export const saveApplication = (data) => {
	return post('/party/application/submit',data);
}

// 获取所有入党志愿书列表
export const getVolunteerList = (data) => {
	return post('/party/volunteer/selectPage',data);
}

// 保存入党申请书的修改
export const saveVolunteer = (data) => {
	return put('/party/volunteer/update',data);
}


// 用户提交审核申请
export const userAddPromotion = (data)=> {
	return post('/promotion',data);
}

// 用户获取自己的申请
export const userGetPromotionList = (id)=> {
	return get('/promotion/getByUserId/' + id);
}

// 用户获取自己所属的党支部
export const userGetDept = (id) => {
	return get('/user/getDeptId/' + id);
}

// 获取所有申请
export const getAllPromtion = (data) => {
	return post('/promotion/page',data);
}

// 更新申请表格
export const updatePromotion = (data) => {
	return put('/promotion',data);
}

// 根据用户id返回会议id表
export const getMeetingIdsByUserIds = (userId) => {
	return get('/userMeeting/meetingIds/' + userId);
}

// 根据会议id返回会议详情
export const getMeetingDetail = (id) => {
	return get('/meetingLog/' + id);
}

// 根据用户id返回谈话列表
export const getTalkList = (id) => {
	return get('/talk/selectByUserId/' + id);
}

// 根据用户id返回信息列别
export const getMessageByUserId = (id) => {
	return get('/informationHint/user/' + id);
}